﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace haber
{
    public partial class Default : System.Web.UI.Page
    {
        public int? SecilikategoriID;
        public static string GetSiteRoot2()
        {
            string port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
            if (port == null || port == "80" || port == "443")
                port = "";
            else
                port = ":" + port;

            string protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
            if (protocol == null || protocol == "0")
                protocol = "http://";
            else
                protocol = "https://";

            string sOut = protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + System.Web.HttpContext.Current.Request.ApplicationPath;

            if (sOut.EndsWith("/"))
            {
                sOut = sOut.Substring(0, sOut.Length - 1);
            }

            return sOut;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
  
                if (Session["seciliKategori"] != null)
                {
                    SecilikategoriID = int.Parse(Session["seciliKategori"].ToString());
                }
            

            if (Request["hid"] != null)
            {
                int hi = -1;
                if (int.TryParse(Request["hid"], out hi))
                {
                    int kategori = -1;
                    if (Request["kat"] != null)
                    {
                        if (int.TryParse(Request["kat"], out kategori))
                        {
                            SecilikategoriID = kategori;
                        }
                    }
                    else
                    {
                            using (dcHaberDataContext dc = new dcHaberDataContext())
                            {
                                var kats= from k in dc.HaberGonderims
                                               where k.HaberID == hi
                                               select k;
                                if (kats.Count() > 0)
                                {
                                    SecilikategoriID = kats.First().KategoriID;
                                }
                            }
                    }
                    using (dcHaberDataContext dc = new dcHaberDataContext())
                    {
                        var baslik = from b in dc.Habers
                                     where b.HaberID == hi
                                     select b;
                        var ekler = from eks in dc.HaberEklers
                                    where eks.HaberID == hi
                                    select eks;
                        if (baslik.Count() > 0)
                        {
                            Haber h = baslik.First();
                            Page p = this.Page;
                            p.Header.Description = h.Baslik;
                            HtmlMeta hm;
                            hm = new HtmlMeta();
                            hm.Name = "og:title";
                            hm.Content = h.Baslik;
                            Page.Header.Controls.AddAt(0, hm);
                           
                
                            hm = new HtmlMeta();
                            hm.Name = "og:description";
                            hm.Content = h.Ozet;
                            Page.Header.Controls.AddAt(0, hm);

                            hm = new HtmlMeta();
                            hm.Name = "og:type";
                            hm.Content = "article";
                            Page.Header.Controls.AddAt(0, hm);

                            hm = new HtmlMeta();
                            hm.Name = "og:url";
                            hm.Content = GetSiteRoot2() + "?hid=" + h.HaberID;
                            Page.Header.Controls.AddAt(0, hm);
                            if (ekler.Count() > 0) { 
                            if (ekler.First().Tip == 'R')
                            {
                                hm = new HtmlMeta();
                                hm.Name = "og:image";
                                hm.Content = GetSiteRoot2() + "/haberEkleri/" + h.HaberID + "/" + ekler.First().Ek;
                                Page.Header.Controls.AddAt(0, hm);
                            }
                            else
                            {
                                hm = new HtmlMeta();
                                hm.Name = "og:video";
                                hm.Content = GetSiteRoot2() + "/haberEkleri/" + h.HaberID + "/" + ekler.First().Ek;
                                Page.Header.Controls.AddAt(0, hm);
                                string cont = "";
                                if (ekler.First().Ek.Contains("flv"))
                                {
                                    cont = "application/x-shockwave-flash";

                                }
                                else
                                {
                                    cont = "video/mp4";
                                }
                                hm = new HtmlMeta();
                                hm.Name = "og:video:type";
                                hm.Content = cont;
                                Page.Header.Controls.AddAt(0, hm);
                                hm = new HtmlMeta();
                                hm.Name = "og:image";
                                hm.Content = GetSiteRoot2() + "/video.png";
                                Page.Header.Controls.AddAt(0, hm);

                            }
                            }
                            hm = new HtmlMeta();
                            hm.Name = "og:site_name";
                            hm.Content = "sosyalmedyahaber.org";
                            Page.Header.Controls.AddAt(0, hm);

                            hm = new HtmlMeta();
                            hm.Name = "fb:admins";
                            hm.Content = "689431045";
                            Page.Header.Controls.AddAt(0, hm);

                        }
                    }


                }
            }
            else if (Request["tv"] != null)
            {

                String isim = Request["tv"].ToString();
                Page p = this.Page;
                p.Header.Description = isim;
                HtmlMeta hm;
                hm = new HtmlMeta();
                hm.Name = "og:title";
                hm.Content = isim;
                Page.Header.Controls.AddAt(0, hm);


                hm = new HtmlMeta();
                hm.Name = "og:type";
                hm.Content = "website";
                Page.Header.Controls.AddAt(0, hm);


                hm = new HtmlMeta();
                hm.Name = "og:video";
                hm.Content = GetSiteRoot2() + "/haberTV/" + isim;
                Page.Header.Controls.AddAt(0, hm);
                string cont = "";
                if (isim.Contains("flv"))
                {
                    cont = "application/x-shockwave-flash";

                }
                else
                {
                    cont = "video/mp4";
                }

                hm = new HtmlMeta();
                hm.Name = "og:video:type";
                hm.Content = cont;
                Page.Header.Controls.AddAt(0, hm);

                hm = new HtmlMeta();
                hm.Name = "og:url";
                hm.Content = GetSiteRoot2() + "?tv=" + isim;
                Page.Header.Controls.AddAt(0, hm);

                hm = new HtmlMeta();
                hm.Name = "og:image";
                hm.Content = GetSiteRoot2() + "/Styles/default.png";
                Page.Header.Controls.AddAt(0, hm);


                hm = new HtmlMeta();
                hm.Name = "og:site_name";
                hm.Content = "sosyalmedyahaber.org";
                Page.Header.Controls.AddAt(0, hm);

                hm = new HtmlMeta();
                hm.Name = "fb:admins";
                hm.Content = "689431045";
                Page.Header.Controls.AddAt(0, hm);

            }


        }
    }
}